home *** CD-ROM | disk | FTP | other *** search
/ ftp.mactech.com 2010 / ftp.mactech.com.tar / ftp.mactech.com / machack / Hacks97 / WarriorsProgress.sit / Warrior’s Progress / source code / Source / Libraries / Files / FileAccessPath.cp < prev    next >
Text File  |  1997-06-28  |  3KB  |  153 lines

  1. // FileAccessPath.cp
  2.  
  3. #ifndef FileAccessPath_h
  4. #include "FileAccessPath.h"
  5. #endif
  6. #ifndef FileLocation_h
  7. #include "FileLocation.h"
  8. #endif
  9. #ifndef FileNotFoundError_h
  10. #include "FileNotFoundError.h"
  11. #endif
  12. #ifndef DirectoryNotFoundError_h
  13. #include "DirectoryNotFoundError.h"
  14. #endif
  15. #ifndef DiskFullError_h
  16. #include "DiskFullError.h"
  17. #endif
  18. #ifndef FileLockError_h
  19. #include "FileLockError.h"
  20. #endif
  21. #ifndef FileBusyError_h
  22. #include "FileBusyError.h"
  23. #endif
  24. #ifndef FilePermissionError_h
  25. #include "FilePermissionError.h"
  26. #endif
  27. #ifndef HardwareVolumeLockError_h
  28. #include "HardwareVolumeLockError.h"
  29. #endif
  30. #ifndef SoftwareVolumeLockError_h
  31. #include "SoftwareVolumeLockError.h"
  32. #endif
  33.  
  34. FileAccessPath::FileAccessPath()
  35.   : isOpen( false )
  36.   {
  37.   }
  38.  
  39. FileAccessPath::FileAccessPath( const FileLocation& location,
  40.                                           FilePermission permission,
  41.                                           DataFork )
  42.   : isOpen( false )
  43.   {
  44.     Open( location, permission, dataFork );
  45.   }
  46.  
  47. FileAccessPath::FileAccessPath( const FileLocation& location,
  48.                                           FilePermission permission,
  49.                                           ResourceFork )
  50.   : isOpen( false )
  51.   {
  52.     Open( location, permission, resourceFork );
  53.   }
  54.  
  55. FileAccessPath::~FileAccessPath()
  56.   {
  57.     Assert( !IsOpen() );
  58.     if ( IsOpen() )
  59.         try
  60.           {
  61.             Close();
  62.           }
  63.          catch ( ... )
  64.           {
  65.           }
  66.   }
  67.  
  68. void FileAccessPath::Open( const FileLocation& location,
  69.                                     FilePermission permission,
  70.                                     DataFork )
  71.   {
  72.     Assert( !IsOpen() );
  73.     Assert( location.NameIsValid() );
  74.     ThrowError( FSpOpenDF( &location, permission.Value(), &refNum ) );
  75.     isOpen = true;
  76.   }
  77.  
  78. void FileAccessPath::Open( const FileLocation& location,
  79.                                     FilePermission permission,
  80.                                     ResourceFork )
  81.   {
  82.     Assert( !IsOpen() );
  83.     Assert( location.NameIsValid() );
  84.     ThrowError( FSpOpenRF( &location, permission.Value(), &refNum ) );
  85.     isOpen = true;
  86.   }
  87.  
  88. void FileAccessPath::Close()
  89.   {
  90.     Assert( IsOpen() );
  91.     isOpen = false;
  92.     ThrowError( FSClose( refNum ) );
  93.   }
  94.  
  95. uint32 FileAccessPath::Length() const
  96.   {
  97.     Assert( IsOpen() );
  98.     int32 length;
  99.     ThrowError( GetEOF( refNum, &length ) );
  100.     return length;
  101.   }
  102.  
  103. void FileAccessPath::SetLength( uint32 length )
  104.   {
  105.     Assert( IsOpen() );
  106.     ThrowError( SetEOF( refNum, length ) );
  107.   }
  108.  
  109. uint32 FileAccessPath::Allocate( uint32 amount )
  110.   {
  111.     Assert( IsOpen() );
  112.     ThrowError( ::Allocate( refNum, reinterpret_cast<int32*>(&amount) ) );
  113.     return amount;
  114.   }
  115.  
  116. uint32 FileAccessPath::AllocateContiguous( uint32 amount )
  117.   {
  118.     Assert( IsOpen() );
  119.     ThrowError( AllocContig( refNum, reinterpret_cast<int32*>(&amount) ) );
  120.     return amount;
  121.   }
  122.  
  123. void FileAccessPath::Flush() const
  124.   {
  125.     ParamBlockRec block;
  126.     block.ioParam.ioCompletion = 0;
  127.     block.ioParam.ioRefNum = refNum;
  128.     ThrowError( PBFlushFileSync( &block ) );
  129.   }
  130.  
  131. void FileAccessPath::ThrowError( OSErr error )
  132.   {
  133.     if ( error == noErr )
  134.         return;
  135.     
  136.     switch ( error )
  137.       {
  138.         case fnfErr:                throw FileNotFoundError( error );
  139.         case dirNFErr:                throw DirectoryNotFoundError( error );
  140.         case dskFulErr:            throw DiskFullError( error );
  141.         case permErr:                throw FileLockError( error );
  142.         case opWrErr:                throw FileBusyError( error );
  143.         case afpAccessDenied:    throw FilePermissionError( error );
  144.         
  145.         case wPrErr:                throw HardwareVolumeLockError( error );
  146.         case vLckdErr:                throw SoftwareVolumeLockError( error );
  147.         case fLckdErr:                throw FileLockError( error );
  148.         
  149.       }
  150.     
  151.     throw FileError( error );
  152.   }
  153.